package com.cxj.cxj_test_cloud.base.mvvm.view;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.cxj.cxj_test_cloud.R;
import com.cxj.cxj_test_cloud.base.mvvm.viewmodel.BaseViewModel;
import com.cxj.cxj_test_cloud.common.di.Injectable;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import javax.inject.Inject;

import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasAndroidInjector;

public abstract class BaseActivity<V extends ViewDataBinding, VM extends BaseViewModel> extends RxAppCompatActivity implements Injectable, HasAndroidInjector {
    @Inject
    DispatchingAndroidInjector androidInjector;

    protected V mDataBinding;
    protected VM mViewModel;

    @SuppressLint("ObsoleteSdkInt")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //屏幕常亮
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //1.设置内容延伸到状态栏  透明状态栏+文字可见+窗口透明
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);//透明状态栏+文字可见
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        //2.抽象布局，同时让LiveData&Observable拥有生命周期感知能力
        mDataBinding= DataBindingUtil.setContentView(this,getContentViewId());
        if (mDataBinding != null){
            mDataBinding.setLifecycleOwner(this);
        }


        if (needAddStatusBarHeight()){
            //移除所有的View
            ViewGroup contentParent = findViewById(android.R.id.content);
            contentParent.removeAllViews();

            //添加LinearLayout做承载
            LinearLayout l1=new LinearLayout(this);
            l1.setOrientation(LinearLayout.VERTICAL);
            FrameLayout.LayoutParams fl=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
            contentParent.addView(l1,fl);

            //自定义状态栏覆盖
            View view=new View(this);
            view.setBackgroundColor(getStatusBarColors());
            LinearLayout.LayoutParams l2=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,StatusBarUtil.getStatusBarHeight(this));
            l1.addView(view,l2);

            if (mDataBinding !=null){
                l1.addView(mDataBinding.getRoot());
            }else {
                LayoutInflater.from(this).inflate(getContentViewId(),l1);
            }
        }
    }

    private int getStatusBarColors() {
        return Color.WHITE;
    }

    private boolean needAddStatusBarHeight() {
        return true;
    }

    /**
     * 子类返回具体的布局文件
     * @return
     */
    protected abstract int getContentViewId();

    /**
     * 给Fragment/其他对象提供数据
     * @return
     */
    @Override
    public AndroidInjector<Object> androidInjector() {
        return androidInjector;
    }

}

